//
//  AppDelegate.swift
//  FHweibo
//
//  Created by 张嵩雷 on 16/4/8.
//  Copyright © 2016年 feng. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setupAppearance()
        
        //创建账号
//        let account = FHUserAccountViewModel.sharedInstance.userAccount
//        FHPrint("测试账号:\(account)")
        FHPrint("用户是否登录: \(FHUserAccountViewModel.sharedInstance.userLogin)")
        
        //创建window
        window = UIWindow(frame:  UIScreen.mainScreen().bounds)
        
//        let mainVC = FHMainViewController()
        
        
//        设置root控制器
        window?.rootViewController = defaultViewController()
        
        // TODO: 测试
//        window?.rootViewController = FHWelcomeViewController()
//        window?.rootViewController = FHNewFeatureViewController()
      
        
        //设置为主窗口
        //当window有值的时候才往后面执行
        window?.makeKeyAndVisible()
        
//        FHPrint("isNewVersion: \(isNewVersion())")
        
        return true
    }
    
    ///切换控制器,搞一个统一的切换方法,到时候调用方便
    func switchViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    
    ///进入程序设置默认控制器
    private func defaultViewController() -> UIViewController {
        if !FHUserAccountViewModel.sharedInstance.userLogin{
            //没有登录
            return FHMainViewController()
        }
        //表示已经登录
        let newVersion = isNewVersion()

        return newVersion ? FHNewFeatureViewController() : FHWelcomeViewController()
    }
    
    
    
    ///设置UIBarButtonItem皮肤
    private func setupAppearance() {
        let barButtonItemAppearance = UIBarButtonItem.appearance()
        barButtonItemAppearance.tintColor = UIColor.orangeColor()
    }
    
    ///是否是新版本
    ///-returns:是否是新版本
    private func isNewVersion() ->Bool {
        //1.获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
        
        //2.获取上一次的版本号
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        //3.对比
        let newVersion = (currentVersion != sandboxVersion)
        FHPrint("currentVersion: \(currentVersion), sandboxVersion: \(sandboxVersion)")
        
        //4.是新版本,就保存新的版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        //5.返回比较的结果
        return newVersion
        
    }
    
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

